from pyurl.url import BaseURL, URL

class File(object):
    def __init__(self, path, child=None):
        if isinstance(path, BaseURL):
            self.url = path
        else:
            self.url = URL(path)
            
        if child is not None:
            self.url = URL(self.url, child)

    def open(self, mode):
        return self.url.connection.open_file(self.url.path, mode)
    
    def get_content(self):
        if self.exists:
            stream = self.open("r")
            data = stream.read()
            stream.close()
            return data
        else:
            return None
    
    def set_content(self, content):
        stream = self.open("w")
        stream.write(content)
        stream.close()
        
    def get_exists(self):
        return self.url.connection.exists(self.url.path)
    
    def __str__(self):
        return str(self.url)
    
    content = property(get_content, set_content)
    exists = property(get_exists)